(() => {

    'use strict';
    goog.provide('Blockly.Blocks.ruilong');
    goog.require('Blockly.Blocks');
  

   //LCD1602显示模块 
    Blockly.Arduino.forBlock['ruilong_LCD1602_Print'] = function () {
        var varName = this.getFieldValue('VAR');
        var str1 = Blockly.Arduino.valueToCode(this, 'TEXT', Blockly.Arduino.ORDER_ATOMIC) || '\"\"';
        var str2 = Blockly.Arduino.valueToCode(this, 'TEXT2', Blockly.Arduino.ORDER_ATOMIC) || '\"\"';
        var device = Blockly.Arduino.valueToCode(this, 'device', Blockly.Arduino.ORDER_ATOMIC) || '0x27';
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_LiquidCrystal_I2C'] = '#include <LiquidCrystal_I2C.h>';
        Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_' + varName] = 'LiquidCrystal_I2C ' + varName + '(' + device + ',16,2);';
        if (Blockly.Arduino.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_' + varName]) {
            Blockly.Arduino.setups_['setup_lcd_init_' + varName] = varName + '.init();';
            Blockly.Arduino.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
        }
        var code = varName + '.setCursor(0, 0);\n'
        code += varName + '.print(' + str1 + ');\n';
        code += varName + '.setCursor(0, 1);\n';
        code += varName + '.print(' + str2 + ');\n';
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_LCD1602_Print2'] = function () {
        var varName = this.getFieldValue('VAR');
        var str = Blockly.Arduino.valueToCode(this, 'TEXT', Blockly.Arduino.ORDER_ATOMIC) || 'String(\"\")';
        var row = Blockly.Arduino.valueToCode(this, 'row', Blockly.Arduino.ORDER_ATOMIC) || '1';
        var column = Blockly.Arduino.valueToCode(this, 'column', Blockly.Arduino.ORDER_ATOMIC) || '1';
        if (Blockly.Arduino.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_' + varName]) {
            Blockly.Arduino.setups_['setup_lcd_init_' + varName] = varName + '.init();';
            Blockly.Arduino.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
        }
        var code = varName + '.setCursor(' + column + '-1, ' + row + '-1);\n'
        code += varName + '.print(' + str + ');\n';
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_LCD1602_Power'] = function () {
        var varName = this.getFieldValue('VAR');
        var dropdown_stat = this.getFieldValue('STAT');
        if (Blockly.Arduino.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || Blockly.Arduino.definitions_['var_LiquidCrystal_I2C_' + varName]) {
            Blockly.Arduino.setups_['setup_lcd_init_' + varName] = varName + '.init();';
            Blockly.Arduino.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
        }
        var code = varName + '.' + dropdown_stat + '();\n'
        return code;
    };
    //RGB彩灯模块（含灯带，点阵，环形灯等）
    Blockly.Arduino.forBlock['ruilong_rgb_init'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value_ledcount = Blockly.Arduino.valueToCode(this, 'LEDCOUNT', Blockly.Arduino.ORDER_ATOMIC);

        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(' + value_ledcount + ',' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
        Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
        //Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
        return '';
    };
    Blockly.Arduino.forBlock['ruilong_rgb_brightness'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        var code = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_rgb'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value__led_ = Blockly.Arduino.valueToCode(this, '_LED_', Blockly.Arduino.ORDER_ATOMIC);
        var value_rvalue = Blockly.Arduino.valueToCode(this, 'RVALUE', Blockly.Arduino.ORDER_ATOMIC);
        var value_gvalue = Blockly.Arduino.valueToCode(this, 'GVALUE', Blockly.Arduino.ORDER_ATOMIC);
        var value_bvalue = Blockly.Arduino.valueToCode(this, 'BVALUE', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        Blockly.Arduino.definitions_['var_rgb_ruilong' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_ruilong_' + dropdown_rgbpin + '(4,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
        Blockly.Arduino.setups_['setup_rgb_ruilong_begin_' + dropdown_rgbpin] = 'rgb_ruilong_' + dropdown_rgbpin + '.begin();';
        //Blockly.Arduino.setups_['setup_rgb_ruilong_setpin'+dropdown_rgbpin] ='rgb_ruilong_'+dropdown_rgbpin+'.setPin('+dropdown_rgbpin+');';

        var code = 'rgb_ruilong_' + dropdown_rgbpin + '.setPixelColor(' + value__led_ + '-1, ' + value_rvalue + ',' + value_gvalue + ',' + value_bvalue + ');\n';
        code += 'rgb_ruilong_' + dropdown_rgbpin + '.show();\n';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_rgb2'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var value_led = Blockly.Arduino.valueToCode(this, '_LED_', Blockly.Arduino.ORDER_ATOMIC);
        var color = goog.color.hexToRgb(this.getFieldValue('RGB_LED_COLOR'));
        //Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(4,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';

        }
        var code = 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(' + value_led + '-1, ' + color + ');\n';
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_rgb3'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var wait_time = Blockly.Arduino.valueToCode(this, 'WAIT', Blockly.Arduino.ORDER_ATOMIC);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(25,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            '(25,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var funcName2 = 'Wheel';
        var code2 = 'uint32_t Wheel(byte WheelPos) {\n';
        code2 += 'if(WheelPos < 85) {return rgb_display_' + dropdown_rgbpin + '.Color(WheelPos * 3, 255 - WheelPos * 3, 0);} \n';
        code2 += 'else if(WheelPos < 170) {WheelPos -= 85; return rgb_display_' + dropdown_rgbpin + '.Color(255 - WheelPos * 3, 0, WheelPos * 3);}\n ';
        code2 += 'else {WheelPos -= 170;return rgb_display_' + dropdown_rgbpin + '.Color(0, WheelPos * 3, 255 - WheelPos * 3);}\n';
        code2 += '}\n';
        Blockly.Arduino.definitions_[funcName2] = code2;

        var funcName3 = 'rainbow';
        var code3 = 'void rainbow(uint8_t wait) { uint16_t i, j;\n';
        code3 += '      for(j=0; j<256; j++) {               \n';
        code3 += '        for(i=0; i<rgb_display_' + dropdown_rgbpin + '.numPixels(); i++) {\n';
        code3 += '         rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, Wheel((i+j) & 255));}\n';
        code3 += '      rgb_display_' + dropdown_rgbpin + '.show();\n';
        code3 += '      delay(wait);}}\n';

        Blockly.Arduino.definitions_[funcName3] = code3;

        var code = 'rainbow(' + wait_time + ');\n'
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_rgb4'] = function () {
        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var wait_time = Blockly.Arduino.valueToCode(this, 'WAIT', Blockly.Arduino.ORDER_ATOMIC);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(25,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';

            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var funcName2 = 'Wheel';
        var code2 = 'uint32_t Wheel(byte WheelPos) {\n';
        code2 += 'if(WheelPos < 85) {return rgb_display_' + dropdown_rgbpin + '.Color(WheelPos * 3, 255 - WheelPos * 3, 0);} \n';
        code2 += 'else if(WheelPos < 170) {WheelPos -= 85; return rgb_display_' + dropdown_rgbpin + '.Color(255 - WheelPos * 3, 0, WheelPos * 3);}\n ';
        code2 += 'else {WheelPos -= 170;return rgb_display_' + dropdown_rgbpin + '.Color(0, WheelPos * 3, 255 - WheelPos * 3);}\n';
        code2 += '}\n';
        Blockly.Arduino.definitions_[funcName2] = code2;

        var funcName3 = 'rainbow';
        var code3 = 'void rainbow(uint8_t wait) { uint16_t i, j;\n';
        code3 += '      for(j=0; j<256; j++) {               \n';
        code3 += '        for(i=0; i<rgb_display_' + dropdown_rgbpin + '.numPixels(); i++) {\n';
        code3 += '         rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, Wheel((i+j) & 255));}\n';
        code3 += '      rgb_display_' + dropdown_rgbpin + '.show();\n';
        code3 += '      delay(wait);}}\n';

        Blockly.Arduino.definitions_[funcName3] = code3;

        var funcName4 = 'rainbowCycle';
        var code4 = 'void rainbowCycle(uint8_t wait) {uint16_t i, j;\n';
        code4 += '    for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel\n';
        code4 += '      for(i=0; i< rgb_display_' + dropdown_rgbpin + '.numPixels(); i++) {\n';
        code4 += '        rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, Wheel(((i * 256 / rgb_display_' + dropdown_rgbpin + '.numPixels()) + j) & 255));}\n';
        code4 += '     rgb_display_' + dropdown_rgbpin + '.show();\n';
        code4 += '     delay(wait);}}\n';

        Blockly.Arduino.definitions_[funcName4] = code4;

        var code = 'rainbowCycle(' + wait_time + ');\n'
        return code;
    };






    Blockly.Arduino.forBlock['ruilong_tm1650_power'] = function () {
        var stat = this.getFieldValue("STAT");
        Blockly.Arduino.definitions_['dinclude_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_TM1650'] = '#include <TM1650.h>';
        Blockly.Arduino.definitions_['var_ruilong_4display'] = 'TM1650 ruilong_4display;';
        Blockly.Arduino.setups_['setup_wire_begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_ruilong_4display_init'] = 'ruilong_4display.init();';
        return 'ruilong_4display.' + stat + '();\n';
    };
    Blockly.Arduino.forBlock['ruilong_tm1650_displayString'] = function () {
        var value = Blockly.Arduino.valueToCode(this, 'VALUE', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['dinclude_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_TM1650'] = '#include <TM1650.h>';
        Blockly.Arduino.definitions_['var_ruilong_4display'] = 'TM1650 ruilong_4display;';
        Blockly.Arduino.setups_['setup_wire_begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_ruilong_4display_init'] = 'ruilong_4display.init();';
        return 'ruilong_4display.displayString(' + value + ');\n';
    };
    Blockly.Arduino.forBlock['ruilong_tm1650_showDot'] = function () {
        var no = this.getFieldValue("NO");
        var stat = this.getFieldValue("STAT");
        Blockly.Arduino.definitions_['dinclude_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_TM1650'] = '#include <TM1650.h>';
        Blockly.Arduino.definitions_['var_ruilong_4display'] = 'TM1650 ruilong_4display;';
        Blockly.Arduino.setups_['setup_wire_begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_ruilong_4display_init'] = 'ruilong_4display.init();';
        return 'ruilong_4display.setDot(' + no + ',' + stat + ');\n';
    };
    //显示-TM1637-初始化
    Blockly.Arduino.forBlock['ruilong_tm1637_init'] = function () {
        var CLK = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
        var DIO = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_tm1637'] = '#include <SevenSegmentTM1637.h>';
        Blockly.Arduino.definitions_['var_tm1637'] = 'SevenSegmentTM1637 display(' + CLK + ',' + DIO + ');';
        Blockly.Arduino.setups_['setup_tm1637_init'] = '  display.begin();\ndelay(1000); ';
        return '';
    };

    //显示-TM1637-显示字符串（字符，数值、变量）
    Blockly.Arduino.forBlock['ruilong_tm1637_displayString'] = function () {

        var value = Blockly.Arduino.valueToCode(this, 'VALUE', Blockly.Arduino.ORDER_ATOMIC);
        var code = 'display.clear();\n display.print(' + value + ');\n ';
        return code;
    };

    //显示-TM1637-显示时间
    Blockly.Arduino.forBlock['ruilong_tm1637_displayTime'] = function () {
        var CLK = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
        var DIO = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
        var value = Blockly.Arduino.valueToCode(this, 'VALUE', Blockly.Arduino.ORDER_ATOMIC);
        var hour = Blockly.Arduino.valueToCode(this, 'hour', Blockly.Arduino.ORDER_ATOMIC);
        var minute = Blockly.Arduino.valueToCode(this, 'minute', Blockly.Arduino.ORDER_ATOMIC);

        //  Blockly.Arduino.definitions_['var_tm1637'] = 'SevenSegmentExtended display(' + CLK + ',' + DIO + ');';
        var code = '  byte hours =' + hour + ';\n';
        code += '  byte minutes  = ' + minute + ';\n';
        code += '  byte second  = 0;\n';
        code += 'for (; hours < 24; hours++)\n{\nfor ( ; minutes < 60; minutes++)\n{\nfor ( ; second < 60; second++)\n{\ndisplay.printTime(hours, minutes, true);\n}\nsecond=0;\n}\nminutes = 0;\n}\n';
        return code;
    };

    //显示-TM1637-设置亮度
    Blockly.Arduino.forBlock['ruilong_tm1637_Brightness'] = function () {
        var BRIGHTNESS = Blockly.Arduino.valueToCode(this, 'BRIGHTNESS', Blockly.Arduino.ORDER_ASSIGNMENT) || '0';
        // var BRIGHTNESS = this.getFieldValue('BRIGHTNESS');
        var code = '  display.setBacklight(' + BRIGHTNESS + ');\n';
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_display_Matrix_DisplayChar'] = function () {

        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'RGBArray', Blockly.Arduino.ORDER_ASSIGNMENT);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        //Blockly.Arduino.definitions_['RGBArray'] = 'uint8_t  RGBArray[16][3];';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(25,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var code = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');\n';
        code += 'for(int i=0; i<25; i++)\n';
        code += '{\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, ' + dotMatrixArray + '[i][0],' + dotMatrixArray + '[i][1],' + dotMatrixArray + '[i][2]);\n';
        code += '}\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        code += 'delay(100);\n';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_display_Matrix_LedArray'] = function () {
        var varName = this.getFieldValue('VAR');
        var code = '{';
        for (var i = 1; i < 6; i++) {
            for (var j = 1; j < 6; j++) {
                code += '\n          {';
                code += goog.color.hexToRgb(this.getFieldValue('RGB_' + i + j));
                code += '}';
                if (i == 5 && j == 5) { } else code += ','
            }
        }
        code += '};';
        Blockly.Arduino.definitions_[varName] = "uint8_t  " + varName + "[25][3]=" + code;
        return [varName, Blockly.Arduino.ORDER_ATOMIC];
    }
    Blockly.Arduino.forBlock['ruilong_display_Matrix_DisplayLedBar'] = function () {

        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'RGBArray', Blockly.Arduino.ORDER_ASSIGNMENT);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
        //Blockly.Arduino.definitions_['RGBArray'] = 'uint8_t  RGBArray[16][3];';
        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(5,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var code = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');\n';
        code += 'for(int i=0; i<5; i++)\n';
        code += '{\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, ' + dotMatrixArray + '[i][0],' + dotMatrixArray + '[i][1],' + dotMatrixArray + '[i][2]);\n';
        code += '}\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        code += 'delay(100);\n';
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_display_Matrix_LedBar'] = function () {
        var varName = this.getFieldValue('VAR');
        var code = '{';
        for (var j = 1; j < 6; j++) {
            code += '\n          {';
            code += goog.color.hexToRgb(this.getFieldValue('RGB_' + j));
            code += '}';
            if (j == 5) { } else code += ','
        }
        code += '};';
        Blockly.Arduino.definitions_[varName] = "uint8_t  " + varName + "[5][3]=" + code;
        return [varName, Blockly.Arduino.ORDER_ATOMIC];
    }

    Blockly.Arduino.forBlock['ruilong_display_Matrix_DisplayChar2'] = function () {

        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'RGBArray_dot', Blockly.Arduino.ORDER_ASSIGNMENT);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        var color = goog.color.hexToRgb(this.getFieldValue('RGB_DOT_COLOR'));

        // Blockly.Arduino.definitions_['rgb_color'] = "uint8_t  rgb_color[3]={" + color+'}\n;';


        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';

        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(25,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var code = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');\n';
        code += 'for(int i=0; i<25; i++)\n';
        code += '{\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, ' + dotMatrixArray + '[i]*' + color[0] + ',' + dotMatrixArray + '[i]*' + color[1] + ',' + dotMatrixArray + '[i]*' + color[2] + ');\n';
        code += '}\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        code += 'delay(100);\n';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_display_Matrix_LedArray2'] = function () {
        var varName = this.getFieldValue('VAR');
        //var RGB_ = new Array();
        var code = '{';
        for (var i = 1; i < 6; i++) {
            for (var j = 1; j < 6; j++) {
                code += '';
                code += (this.getFieldValue('DOT_' + i + j) == "TRUE") ? 1 : 0;
                code += '';
                if (i == 5 && j == 5) { } else code += ','
            }
        }
        code += '};';

        //Blockly.Arduino.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
        Blockly.Arduino.definitions_[varName] = "uint8_t  " + varName + "[25]=" + code;
        //return ["LedArray_"+clearString(this.id), Blockly.Arduino.ORDER_ATOMIC];
        return [varName, Blockly.Arduino.ORDER_ATOMIC];
    }
    Blockly.Arduino.forBlock['ruilong_display_Matrix_DisplayBar2'] = function () {

        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'RGBArray_dot', Blockly.Arduino.ORDER_ASSIGNMENT);
        var value_brightness = Blockly.Arduino.valueToCode(this, 'NUM', Blockly.Arduino.ORDER_ATOMIC);
        var color = goog.color.hexToRgb(this.getFieldValue('RGB_DOT_COLOR'));

        // Blockly.Arduino.definitions_['rgb_color'] = "uint8_t  rgb_color[3]={" + color+'}\n;';


        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';

        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(5,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
            Blockly.Arduino.setups_['setup_rgb_display_setBrightness' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setBrightness(' + value_brightness + ');';
        }
        var code = '';
        code += 'for(int i=0; i<5; i++)\n';
        code += '{\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, ' + dotMatrixArray + '[i]*' + color[0] + ',' + dotMatrixArray + '[i]*' + color[1] + ',' + dotMatrixArray + '[i]*' + color[2] + ');\n';
        code += '}\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        code += 'delay(100);\n';
        return code;
    };

    Blockly.Arduino.forBlock['ruilong_display_Matrix_LedBar2'] = function () {
        var varName = this.getFieldValue('VAR');
        //var RGB_ = new Array();
        var code = '{';

        for (var j = 1; j < 6; j++) {
            code += '';
            code += (this.getFieldValue('DOT_' + j) == "TRUE") ? 1 : 0;
            code += '';
            if (j == 5) { } else code += ','
        }
        code += '};';

        //Blockly.Arduino.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
        Blockly.Arduino.definitions_[varName] = "uint8_t  " + varName + "[5]=" + code;
        //return ["LedArray_"+clearString(this.id), Blockly.Arduino.ORDER_ATOMIC];
        return [varName, Blockly.Arduino.ORDER_ATOMIC];
    }

    Blockly.Arduino.forBlock['ruilong_display_Matrix_CLEAR'] = function () {

        var dropdown_rgbpin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'RGBArray', Blockly.Arduino.ORDER_ASSIGNMENT);
        Blockly.Arduino.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';

        if (!Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin]) {
            Blockly.Arduino.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '(25,' + dropdown_rgbpin + ', NEO_GRB + NEO_KHZ800);';
            Blockly.Arduino.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
            Blockly.Arduino.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
        }
        var code = '';
        code += 'for(int i=0; i<25; i++)\n';
        code += '{\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(i, 0,0,0);\n';
        code += '}\n'
        code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
        code += 'delay(100);\n';
        return code;
    };


    Blockly.Arduino.forBlock['ruilong_88display_Matrix_blink_rate'] = function () {
        var matrixName = this.getFieldValue('matrixName');
        var BLINK_RATE = Blockly.Arduino.valueToCode(this, 'Blink_rate', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';
        var code = '';
        code += matrixName + '.blinkRate(' + BLINK_RATE + ');\n';
        return code;
    };
    //辅助块_点阵屏_清除显示
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_brightness'] = function () {
        var matrixName = this.getFieldValue('matrixName');
        var BRIGHTNESS = Blockly.Arduino.valueToCode(this, 'Brightness', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';
        var code = '';
        code += matrixName + '.setBrightness(' + BRIGHTNESS + ');\n';
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_POS'] = function () {
        var pos_x = Blockly.Arduino.valueToCode(this, 'XVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y = Blockly.Arduino.valueToCode(this, 'YVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var matrixName = this.getFieldValue('matrixName');
        var dropdown_type = this.getFieldValue('DrawPixel_TYPE');

        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';

        var code = matrixName + '.drawPixel(' + pos_x + '-1,' + pos_y + '-1,' + dropdown_type + ');\n'
        code += matrixName + '.writeDisplay();\n';
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_drawLine'] = function () {

        var pos_x0 = Blockly.Arduino.valueToCode(this, 'X0VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y0 = Blockly.Arduino.valueToCode(this, 'Y0VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_x1 = Blockly.Arduino.valueToCode(this, 'X1VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y1 = Blockly.Arduino.valueToCode(this, 'Y1VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var matrixName = this.getFieldValue('matrixName');
        var dropdown_type = this.getFieldValue('DrawPixel_TYPE');

        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';

        var code = matrixName + '.drawLine(' + pos_x0 + '-1,' + pos_y0 + '-1,' + pos_x1 + '-1,' + pos_y1 + '-1,' + dropdown_type + ');\n'
        code += matrixName + '.writeDisplay();\n';

        return code;
    };

    Blockly.Arduino.forBlock['ruilong_88display_Matrix_drawCircle'] = function () {
        var drawCircle_type = this.getFieldValue('drawCircle_TYPE');
        var pos_x = Blockly.Arduino.valueToCode(this, 'XVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y = Blockly.Arduino.valueToCode(this, 'YVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_r = Blockly.Arduino.valueToCode(this, 'RVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var matrixName = this.getFieldValue('matrixName');
        var dropdown_type = this.getFieldValue('DrawPixel_TYPE');

        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';

        if (drawCircle_type == 0) {
            var code = matrixName + '.drawCircle(' + pos_x + '-1,' + pos_y + '-1,' + pos_r + ',' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        } else {
            var code = matrixName + '.fillCircle(' + pos_x + '-1,' + pos_y + '-1,' + pos_r + ',' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        }

        return code;
    };

    Blockly.Arduino.forBlock['ruilong_88display_Matrix_drawRect'] = function () {
        var drawCircle_type = this.getFieldValue('drawRect_TYPE');
        var pos_x = Blockly.Arduino.valueToCode(this, 'XVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y = Blockly.Arduino.valueToCode(this, 'YVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_w = Blockly.Arduino.valueToCode(this, 'WVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_h = Blockly.Arduino.valueToCode(this, 'HVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var matrixName = this.getFieldValue('matrixName');
        var dropdown_type = this.getFieldValue('DrawPixel_TYPE');

        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';

        if (drawCircle_type == 0) {
            var code = matrixName + '.drawRect(' + pos_x + '-1,' + pos_y + '-1,' + pos_w + ',' + pos_h + ',' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        } else {
            var code = matrixName + '.fillRect(' + pos_x + '-1,' + pos_y + '-1,' + pos_w + ',' + pos_h + ',' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        }

        return code;
    };

    Blockly.Arduino.forBlock['ruilong_88display_Matrix_drawRoundRect'] = function () {
        var drawCircle_type = this.getFieldValue('drawRoundRect_TYPE');
        var pos_x = Blockly.Arduino.valueToCode(this, 'XVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y = Blockly.Arduino.valueToCode(this, 'YVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_w = Blockly.Arduino.valueToCode(this, 'WVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_h = Blockly.Arduino.valueToCode(this, 'HVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_r = Blockly.Arduino.valueToCode(this, 'RVALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var matrixName = this.getFieldValue('matrixName');
        var dropdown_type = this.getFieldValue('DrawPixel_TYPE');

        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';

        if (drawCircle_type == 0) {
            var code = matrixName + '.drawRoundRect(' + pos_x + '-1,' + pos_y + '-1,' + pos_w + ',' + pos_h + ',' + pos_r + ',' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        } else {
            var code = matrixName + '.fillRoundRect(' + pos_x + '-1,' + pos_y + '-1,' + pos_w + ',' + pos_h + ',' + pos_r + ',' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        }

        return code;
    };


    Blockly.Arduino.forBlock['ruilong_88display_Matrix_drawTriangle'] = function () {
        var drawCircle_type = this.getFieldValue('drawRoundRect_TYPE');
        var pos_x0 = Blockly.Arduino.valueToCode(this, 'X0VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y0 = Blockly.Arduino.valueToCode(this, 'Y0VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_x1 = Blockly.Arduino.valueToCode(this, 'X1VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y1 = Blockly.Arduino.valueToCode(this, 'Y1VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_x2 = Blockly.Arduino.valueToCode(this, 'X2VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var pos_y2 = Blockly.Arduino.valueToCode(this, 'Y2VALUE', Blockly.Arduino.ORDER_ASSIGNMENT);
        var matrixName = this.getFieldValue('matrixName');
        var dropdown_type = this.getFieldValue('DrawPixel_TYPE');

        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';

        if (drawCircle_type == 0) {
            var code = matrixName + '.drawTriangle(' + pos_x0 + '-1,' + pos_y0 + '-1,' + pos_x1 + '-1,' + pos_y1 + '-1,' + pos_x2 + '-1,' + pos_y2 + '-1,' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        } else {
            var code = matrixName + '.fillTriangle(' + pos_x0 + '-1,' + pos_y0 + '-1,' + pos_x1 + '-1,' + pos_y1 + '-1,' + pos_x2 + '-1,' + pos_y2 + '-1,' + dropdown_type + ');\n'
            code += matrixName + '.writeDisplay();\n';
        }

        return code;
    };

    /*Blockly.Arduino.forBlock['ruilong_88display_Matrix_Rotation'] = function() {
      var matrixName = this.getFieldValue('matrixName');
      var dropdown_type = this.getFieldValue('Rotation_TYPE');
      Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';                 
      Blockly.Arduino.definitions_[matrixName] =  'HT16K33  '+ matrixName +'; ';
      Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';
      
      var code = matrixName + '.setHf_flip('+dropdown_type+');\n'
      return code;
    };
    */
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_TEXT'] = function () {
        var matrixName = this.getFieldValue('matrixName');
        var textString = Blockly.Arduino.valueToCode(this, 'TEXT', Blockly.Arduino.ORDER_ASSIGNMENT);
        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';
        var code = matrixName + '.drawStr(' + textString + ');\n'
        return code;
    };
    //执行器_点阵屏显示_显示图案
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_DisplayChar'] = function () {
        var matrixName = this.getFieldValue('matrixName');
        var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'LEDArrayX', Blockly.Arduino.ORDER_ASSIGNMENT);
        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';


        Blockly.Arduino.definitions_['LEDArray'] = 'uint8_t  LEDArray[8];';
        var code = '';
        code += 'for(int i=0; i<8; i++)\n';
        code += '{\n'
        code += '  LEDArray[i]=' + dotMatrixArray + '[i];\n';
        code += '  for(int j=7; j>=0; j--)\n'
        code += '  {\n'
        code += '    if((LEDArray[i]&0x01)>0)\n';
        code += '    ' + matrixName + '.drawPixel(j, i,1);\n';
        code += '    LEDArray[i] = LEDArray[i]>>1;\n';
        code += '  }  \n'
        code += '}\n'
        code += matrixName + '.writeDisplay();\n'
        return code;
    };

    //执行器_点阵屏显示_显示图案
    Blockly.Arduino.forBlock['ruilong_816display_Matrix_DisplayChar'] = function () {
        var matrixName = this.getFieldValue('matrixName');
        var dotMatrixArray = Blockly.Arduino.valueToCode(this, 'LEDArrayX', Blockly.Arduino.ORDER_ASSIGNMENT);
        Blockly.Arduino.definitions_['include_RL_HT16K33'] = '#include <RL_HT16K33.h>';
        Blockly.Arduino.definitions_[matrixName] = 'HT16K33  ' + matrixName + '; ';
        Blockly.Arduino.setups_['setup_' + matrixName] = matrixName + '.begin(0x70); \n';

        Blockly.Arduino.definitions_['LEDArray'] = 'uint16_t  LEDArray[8];';
        var code = '';
        code += 'for(int i=0; i<8; i++)\n';
        code += '{\n'
        code += '  LEDArray[i]=' + dotMatrixArray + '[i];\n';
        code += '  for(int j=15; j>=0; j--)\n'
        code += '  {\n'
        code += '    if((LEDArray[i]&0x01)>0){\n';
        code += '    ' + matrixName + '.drawPixel(j,i,1); }\n';
        code += '    LEDArray[i] = LEDArray[i]>>1;\n';
        code += '  }  \n'
        code += '}\n'
        code += matrixName + '.writeDisplay();\n'
        return code;
    };
    //执行器_点阵屏显示_点阵数组
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_LedArray'] = function () {
        var varName = this.getFieldValue('VAR');
        var a = new Array();
        for (var i = 1; i < 9; i++) {
            a[i] = new Array();
            for (var j = 1; j < 9; j++) {
                a[i][j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
            }
        }
        var code = '{';
        for (var i = 1; i < 9; i++) {
            var tmp = ""
            for (var j = 1; j < 9; j++) {
                tmp += a[i][j];
            }
            tmp = (parseInt(tmp, 2)).toString(16)
            if (tmp.length == 1) tmp = "0" + tmp;
            code += '0x' + tmp + ((i != 8) ? ',' : '');
        }
        code += '};';
        //Blockly.Arduino.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
        Blockly.Arduino.definitions_[varName] = "uint8_t " + varName + "[8]=" + code;
        //return ["LedArray_"+clearString(this.id), Blockly.Arduino.ORDER_ATOMIC];
        return [varName, Blockly.Arduino.ORDER_ATOMIC];
    };
    //显示--选择预设8*8图案
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_Ledimg'] = function () {
        var dropdown_img_ = this.getFieldValue('img_');
        var code = '"' + dropdown_img_ + '"';
        code = '{';
        for (var i = 0; i < 15; i += 2) {
            code += '0x' + dropdown_img_.substr(i, 2) + ((i != 14) ? ',' : '');
        }
        code += '};\n';
        Blockly.Arduino.definitions_['matrix_img_' + dropdown_img_] = "byte " + 'matrix_img_' + dropdown_img_ + "[]=" + code;
        return ['matrix_img_' + dropdown_img_, Blockly.Arduino.ORDER_ATOMIC];
    };
    //执行器_点阵屏显示_图案数组
    Blockly.Arduino.forBlock['ruilong_816display_Matrix_LedArray'] = function () {
        var varName = this.getFieldValue('VAR');
        var a = new Array();
        for (var i = 1; i < 9; i++) {
            a[i] = new Array();
            for (var j = 1; j < 17; j++) {
                a[i][j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
            }
        }
        var code = '{';
        for (var i = 1; i < 9; i++) {
            var tmp = ""
            for (var j = 1; j < 17; j++) {
                tmp += a[i][j];
            }
            tmp = (parseInt(tmp, 2)).toString(16)
            //  alert(tmp);
            if (tmp.length == 1)
                tmp = "000" + tmp;
            else if (tmp.length == 2)
                tmp = "00" + tmp;
            else if (tmp.length == 3)
                tmp = "0" + tmp;
            code += '0x' + tmp + ((i != 8) ? ',' : '');
        }
        code += '};';
        //Blockly.Arduino.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
        Blockly.Arduino.definitions_[varName] = "uint16_t " + varName + "[8]=" + code;
        //return ["LedArray_"+clearString(this.id), Blockly.Arduino.ORDER_ATOMIC];
        return [varName, Blockly.Arduino.ORDER_ATOMIC];
    };
    //显示--选择预设8*16图案
    Blockly.Arduino.forBlock['ruilong_816display_Matrix_Ledimg'] = function () {
        var dropdown_img_ = this.getFieldValue('img_');
        var code = '"' + dropdown_img_ + '"';
        code = '{';
        for (var i = 0; i < 29; i += 4) {
            code += '0x' + dropdown_img_.substr(i, 4) + ((i != 28) ? ',' : '');
        }
        code += '};\n';
        Blockly.Arduino.definitions_['matrix_img_' + dropdown_img_] = "uint16_t  " + 'matrix_img_' + dropdown_img_ + "[]=" + code;
        return ['matrix_img_' + dropdown_img_, Blockly.Arduino.ORDER_ATOMIC];
    };
    //清屏
    Blockly.Arduino.forBlock['ruilong_88display_Matrix_CLEAR'] = function () {
        var matrixName = this.getFieldValue('matrixName');
        var code = matrixName + '.clear();\n'
        code += matrixName + '.writeDisplay();\n';
        return code;
    };
    



  })();